package com.ynny.Dao;


import com.ynny.Dao.ScoreManagerDao;
import com.ynny.Entity.Scores;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class Scorelmpl implements ScoreManagerDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int addScore(String number, String course, Double score) {
        String sql = "insert into scores values(?,?,?)";
        return jdbcTemplate.update(sql, number, course, score);
    }

    @Override
    public Scores queryScoreByNumber(String number) {
        String sql = "select * from scores where number = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Scores.class), number);
    }

    @Override
    public List<Scores> queryAllScore() {
        String sql = "select * from scores";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Scores.class));
    }

    @Override
    public int updateScore(String number, String course, Double score) {
        String sql = "update scores set " + course + " = ? where number = ?";
        return jdbcTemplate.update(sql, score, number);
    }
}
